unit uPesquisaPadrao;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  Grids,
  DBGrids,
  DB,
  ADODB,
  uDataModule,
  ExtCtrls;

type
  Tfm_Pesquisa_Padrao = class(TForm)
    dbg_Pesq_Padrao: TDBGrid;
    qy_Padrao: TADOQuery;
    ds_Padrao: TDataSource;
    Panel1: TPanel;
    ed_Texto_Filtro: TEdit;
    cb_NomeCampo: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure dbg_Pesq_PadraoDblClick(Sender: TObject);
    private
    vCampo,
    vTabela,
    vTabelaJoin,
    vCampoJoin:string;
    vAdoConn:TADOConnection;
    { Private declarations }
  public
    function Pesquisa(pAdoConn:TADOConnection;
                      pTabela,
                      pCampoPesquisa,
                      pCampoRetorno,
                      pTextoOriginal:string;
                      pTabelaJoin:string='';
                      pCampoJoin:string=''): string;
    function  GetStringsIndex(pStrings:TStrings; pFiltro: string):integer;
    procedure Filtra(pTabela,pCampo:string);
    procedure ObjFiltra(Sender:TObject);
    { Public declarations }
  end;

var
  fm_Pesquisa_Padrao: Tfm_Pesquisa_Padrao;

implementation

uses
  uUtils,
  StrUtils;

{$R *.dfm}

procedure Tfm_Pesquisa_Padrao.dbg_Pesq_PadraoDblClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure Tfm_Pesquisa_Padrao.ObjFiltra(Sender:TObject);
begin
  Filtra(vTabela,vCampo);
end;

procedure Tfm_Pesquisa_Padrao.Filtra(pTabela,pCampo:string);
var
  str:string;
begin
  qy_Padrao.Active := false;
  qy_Padrao.SQL.Clear;
  qy_Padrao.SQL.Add('select a.* from '+pTabela+' a');
  if vTabelaJoin <>'' then
    qy_Padrao.SQL.Add('where Exists(select 1 from '+vTabelaJoin+' b where b.'+vCampoJoin+' = a.nr_sequencia)')
  else
    qy_Padrao.SQL.Add('where 1=1 ');
  if ed_Texto_Filtro.Text <> '' then
  begin
    str := StringReplace(ed_Texto_Filtro.Text,' ','%',[rfIgnoreCase,rfReplaceAll]);
    qy_Padrao.SQL.Add('and '+IfThen(cb_NomeCampo.ItemIndex<>-1,cb_NomeCampo.Items[cb_NomeCampo.ItemIndex],pCampo)+' like '+QuotedStr('%'+str+'%'));
  end;
  qy_Padrao.Active := true;
end;

function Tfm_Pesquisa_Padrao.Pesquisa(pAdoConn:TADOConnection;
                                      pTabela,
                                      pCampoPesquisa,
                                      pCampoRetorno,
                                      pTextoOriginal:string;
                                      pTabelaJoin:string='';
                                      pCampoJoin:string=''): string;
begin
  vCampo        := pCampoPesquisa;
  vTabela       := pTabela;
  vTabelaJoin   := pTabelaJoin;
  vCampoJoin    := pCampoJoin;
  vAdoConn      := pAdoConn;
  if Self.ShowModal = mrOk then
    //if vTabelaJoin <> '' then
      //result :=  qy_Padrao.FieldByName('a.'+pCampoRetorno).AsString
    //else
      Result := qy_Padrao.FieldByName(pCampoRetorno).AsString
  else
    result := pTextoOriginal;
end;

procedure Tfm_Pesquisa_Padrao.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure Tfm_Pesquisa_Padrao.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    13:ModalResult := mrOk;
    27:begin
         ModalResult := mrCancel;
         Self.Close;
       end;
  end;
end;

procedure Tfm_Pesquisa_Padrao.FormShow(Sender: TObject);
var
  i:integer;
begin
  qy_Padrao.Connection := vAdoConn;
  dbg_Pesq_Padrao.Columns.Clear;
  ed_Texto_Filtro.OnChange := ObjFiltra;
  ed_Texto_Filtro.SetFocus;
  cb_NomeCampo.Items.Clear;
  ObjFiltra(Nil);
  for i:=0 to qy_Padrao.FieldCount -1 do
  begin
    cb_NomeCampo.Items.Add(qy_Padrao.Fields[i].FieldName);
    dbg_Pesq_Padrao.Columns.Add;
    dbg_Pesq_Padrao.Columns[i].FieldName := qy_Padrao.Fields[i].FieldName;
  end;
  cb_NomeCampo.ItemIndex := GetStringsIndex(cb_NomeCampo.Items,vCampo);
  //AutoSizeDbGridCol(dbg_Pesq_Padrao);
end;

function Tfm_Pesquisa_Padrao.GetStringsIndex(pStrings:TStrings; pFiltro: string):integer;
var
  i:integer;
begin
  result := -1;
  for i:=0 to pStrings.Count-1 do
  begin
    if AnsiLowerCase(pStrings[i]) = AnsiLowerCase(pFiltro) then
    begin
      result := i;
      Exit;
    end;
  end;
end;
end.
